﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace oceanho.webapi.factory.Admin
{
    using oceanho.webapi.factory.httpCore;
    using oceanho.webapi.factory.httpUtility;
    using oceanho.webapi.factory.httpEntity;

    public partial class Get_ReqCacheItem : System.Web.UI.Page
    {
        private bool __ = false;
        private string cmd = string.Empty;
        private string reqId = string.Empty;
        protected string reqID = string.Empty;
        protected string domain = string.Empty;
        protected string ipaddr = string.Empty;
        protected ReqCache reqCache = default(ReqCache);

        protected void Page_Load(object sender, EventArgs e)
        {
            this.cmd = Request.Params["cmd"];
            this.reqId = Request.Params["reqID"];

            this.domain = Request.Form["domain"];
            __ = !string.IsNullOrEmpty(this.domain);
            this.domain = string.IsNullOrEmpty(this.domain) ? Request.Params["domain"] : this.domain;

            this.ipaddr = Request.Form["ipaddr"];
            this.ipaddr = string.IsNullOrEmpty(this.ipaddr) ? Request.Params["ipaddr"] : this.ipaddr;

            this.ipaddr = string.IsNullOrEmpty(this.ipaddr) ? "::1" : this.ipaddr;
            if (!string.IsNullOrEmpty(this.domain) && !string.IsNullOrEmpty(this.ipaddr))
            {
                reqID = MD5Utility.Encrypt(domain + ipaddr, MD5Mode.Default); // RequestUtility.GetSchemeHostAndPort(Request);
                reqCache = ReqCacheDbUtility.GetReqCache(reqID);
            }

            if (!string.IsNullOrEmpty(cmd) && !__)
            {
                bool result
                    = (cmd == "del") ? ReqCacheDbUtility.DeleteReqCache(reqId)                                  // 删除缓存
                    : (cmd == "set") ? ReqCacheDbUtility.RemoveReqCacheIsBadIP(reqId)                           // 移除黑名单
                    : (cmd == "set1") ? ReqCacheDbUtility.AddToIsBadIPForever(reqId)                            // 永久性黑名单
                    : (cmd == "set2") ? ReqCacheDbUtility.SetReqCacheIsValidIPForever(reqId) : false;           // 永久性非黑名单
            }
        }
    }
}